package com.zhentao.service.Impl;

import com.alibaba.fastjson.JSON;
import com.zhentao.api.article.ArticleClientApi;
import com.zhentao.mapper.WmChannelMapper;
import com.zhentao.mapper.WmNewsMapper;
import com.zhentao.mapper.WmUserMapper;
import com.zhentao.model.article.dto.ArticleDto;
import com.zhentao.model.dtos.ResponseResult;
import com.zhentao.model.wemedia.pojo.WmChannel;
import com.zhentao.model.wemedia.pojo.WmNews;
import com.zhentao.model.wemedia.pojo.WmUser;
import com.zhentao.service.WmNewsAutoScanService;
import com.zhentao.utils.aliyun.TextModerationPlusUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 桃
 */
@Service
public class WmNewsAutoScanServiceImpl implements WmNewsAutoScanService {


    @Autowired
    private WmNewsMapper wmNewsMapper;

    @Autowired
    private ArticleClientApi articleClientApi;

    @Autowired
    private WmChannelMapper channelMapper;
    @Autowired
    private WmUserMapper wmUserMapper;

    /**
     * 审核
     *
     * @param id
     */
    @Override
    public void autoScanWmNews(Integer id) {
        WmNews wmNews = wmNewsMapper.selectById(id);
        //获取内容
        if (wmNews != null) {
            String title = wmNews.getTitle();

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.append(title);

            List<Map> maps = JSON.parseArray(wmNews.getContent(), Map.class);
            //传递过来是个图片
            for (Map map : maps) {
                if (map.get("type").equals("text")) {
                    //把获取到的图片保存到images里面 -list集合
                    stringBuilder.append((String) map.get("value"));
                }
            }

            ResponseResult result = TextModerationPlusUtil.checkContent(stringBuilder.toString());
//
            if (result.getCode() == 600) {
                wmNews.setStatus((short) 8);
                wmNews.setReason(result.getErrorMessage());
                wmNewsMapper.updateById(wmNews);
                //审核通过之后--》需要同步信息
                ArticleDto articleDto = new ArticleDto();
                BeanUtils.copyProperties(wmNews, articleDto);

                //设置同步id
                articleDto.setId(wmNews.getArticleId());

                //作者id和作者名字  -根据作者的id查询出作者的名字
                WmUser wmUser = wmUserMapper.selectById(wmNews.getUserId());
                articleDto.setAuthorName(wmUser.getName());

                articleDto.setAuthorId(wmNews.getUserId().longValue());
                articleDto.setLayout(wmNews.getType());

                //频道名字
                WmChannel wmChannel = channelMapper.selectById(wmNews.getChannelId());
                if (wmChannel != null) {
                    articleDto.setChannelName(wmChannel.getName());
                }

                //完成同步操作
                System.out.println("articleDto = " + articleDto);
                ResponseResult result1 = articleClientApi.saveArticle(articleDto);
                System.out.println("responseResult ================================== " + result1.getCode());
                if (result1.getCode() == 200) {
                    //进行关联
                    wmNews.setArticleId((Long) result1.getData());
                    //是否发布
                    Date date = wmNews.getPublishTime();
                    if (!date.after(new Date())) {
                        wmNews.setStatus((short) 9);
                    }

                    wmNewsMapper.updateById(wmNews);
                }

            } else if (result.getCode() == 602) {
                wmNews.setStatus((short) 3);
                wmNewsMapper.updateById(wmNews);
            } else {
                wmNews.setStatus((short) 2);
                wmNews.setReason(result.getErrorMessage());
                wmNewsMapper.updateById(wmNews);
            }
        }
    }
}
